package com.sf.dlb.service;

import com.google.gson.Gson;
import com.sf.dlb.bean.SmsRquestBean;
import com.sf.dlb.config.Global;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.jpa.model.SmsInfo;
import com.sf.dlb.jpa.repository.SmsInfoRepository;
import com.sf.dlb.utils.*;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by jing.tan on 2018/12/10.
 */
@Service
public class SmsInfoService {
    private static Logger logger = LoggerFactory.getLogger(SmsInfoService.class);

    @Autowired
    private SmsInfoRepository smsInfoRepository;

    @Resource
    private Global global;

    /**
     * 获取验证码
     */
    public ResultVO getVerificationCode(SmsRquestBean smsRquestBean){
        logger.info("==getVerificationCode start smsRquestBean={}",GsonUtils.getInstance().toJson(smsRquestBean));
        String smsCode= IdentifyCodeUtil.getRandom(6);
        smsRquestBean.setValidationCode(smsCode);
        StringBuffer smsMessageBuf=new StringBuffer();
        smsMessageBuf.append("您的验证码为：").append(smsCode).append("，20分钟内有效。").append("本信息由系统自动发出，如非本人操作请忽略。");
        smsRquestBean.setSentMessage(smsMessageBuf.toString());
        Gson gson=new Gson();
        String smsJson=gson.toJson(smsRquestBean);
        String respJson=HttpClientUtil.postFromJson(global.sms_service_url,smsJson);
        logger.info("getVerificationCode is json = "+smsJson);
        logger.info("getVerificationCode is respJson = "+respJson);
        if(StringUtils.isEmpty(respJson)){
            return ResultVOUtil.error(ResponseConstant.ERROR,"验证码发送失败");
        }
        ResultVO resultVO;
        try {
            resultVO= gson.fromJson(respJson, ResultVO.class);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("验证码发送异常");
            return ResultVOUtil.error(ResponseConstant.ERROR,"验证码发送失败");
        }
        return resultVO;
    }
    /**
     * 校验验证码
     */
    public ResultVO checkoutVerificationCode(SmsRquestBean smsRquestBean){
        return checkoutVerificationCode(smsRquestBean.getMobile(),smsRquestBean.getValidationCode());
    }
    public ResultVO checkoutVerificationCode(String mobile, String smsCode){
        logger.info("==checkoutVerificationCode start mobile={},smsCode={}",mobile,smsCode);
        SmsInfo smsInfo=smsInfoRepository.findByMobileAndSmsValidationCodeAndSmsMessageStatusAndDelFlag(mobile,smsCode,"0","0","0");
        if(null==smsInfo){
            return ResultVOUtil.error(ResponseConstant.ERROR,"验证码错误");
        }
        logger.info("checkoutVerificationCode smsInfo"+ GsonUtils.getInstance().toJson(smsInfo));
        Date createDate=smsInfo.getCreateDate();
        Date currentDate=new Date();
        Long timeDifference=(currentDate.getTime()-createDate.getTime())/1000;
        if(timeDifference>Long.valueOf(global.sms_validation_code_time)){
            return ResultVOUtil.error(ResponseConstant.ERROR,"验证码已过期");
        }
        //验证通过，此验证码，就过期了
        smsInfo.setUseState("1");
        smsInfoRepository.save(smsInfo);
        return ResultVOUtil.success();
    }
}
